package com.itheima.reggie.controller;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.OssTemplate;
import com.itheima.reggie.common.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传控制器
 */
@RestController
@Slf4j
@RequestMapping(value = "/common")
public class FileUploadController {

    @Autowired
    private OssTemplate ossTemplate;

    @PostMapping(value = "/upload")
    public ResultInfo uploadFile(MultipartFile file){
        try {
            String path = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
            log.info("文件上传路径为:"+path);
            return ResultInfo.success(path);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("文件上传失败");
        }
    }
}
